package com.chandra.pos.customer.web;


import com.chandra.pos.common.web.POSController;
import com.chandra.pos.customer.model.CustomerFilter;
import com.chandra.pos.customer.model.CustomerList;
import com.chandra.pos.customer.model.CustomerModel;
import com.chandra.pos.customer.service.CustomerManager;
import com.chandra.pos.payment.model.PaymentModel;
import com.chandra.pos.payment.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityNotFoundException;

/**
 * Created by Admin on 7/2/14.
 */
@Controller
@RequestMapping("customer")
public class CustomerController extends POSController {


    Logger logger = LoggerFactory.getLogger(CustomerController.class);


    private static String PAGE_MODULE = "customer";


    @Autowired
    private CustomerManager customerManager;

    @Autowired
    private PaymentService paymentService;

    public CustomerController() {
        this.setTabIndex(3);

    }


    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public String index(CustomerModel model) {

        return getPageURL(PAGE_MODULE, PAGE_HOME);

    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(CustomerModel model) {

        return getPageView(PAGE_MODULE, PAGE_HOME);

    }


    @RequestMapping(value = "search", method = RequestMethod.GET)
    public
    @ResponseBody
    CustomerList search(CustomerFilter filter) {


        return customerManager.search(filter);


    }


    @RequestMapping(value = "save", method = RequestMethod.POST)
    public
    @ResponseBody
    CustomerModel save(CustomerModel customerModel) {


        customerModel = customerManager.saveCustomer(customerModel);
        return customerModel;

    }

    @RequestMapping(value = "savePayment", method = RequestMethod.POST)
    public
    @ResponseBody
    PaymentModel savePayment(PaymentModel paymentModel) {

        paymentModel = paymentService.saveCustomerPayment(paymentModel);
        return paymentModel;

    }
    @RequestMapping(value = "saveRefund", method = RequestMethod.POST)
    public
    @ResponseBody
    PaymentModel saveRefund(PaymentModel paymentModel) {


        paymentModel = paymentService.saveCustomerRefund(paymentModel);
        return paymentModel;

    }


    @RequestMapping(value = "getDetails", method = RequestMethod.GET)
    public
    @ResponseBody
    CustomerModel getDetails(CustomerFilter filter) {

        CustomerModel employeeModel = new CustomerModel();
        try {

            employeeModel = customerManager.getCustomerDetail(filter);
        } catch (EntityNotFoundException e) {

        }

        return employeeModel;
    }
}